<?php

/* Create the Proptery Custom Post Type */
function create_property_post_type() 
{
	$labels = array(
		'name' => __( 'Properties','locality'),
		'singular_name' => __( 'Property','locality' ),
		'add_new' => __('Add New','locality'),
		'add_new_item' => __('Add New Portfolio','locality'),
		'edit_item' => __('Edit Property','locality'),
		'new_item' => __('New Property','locality'),
		'view_item' => __('View Property','locality'),
		'search_items' => __('Search Property','locality'),
		'not_found' =>  __('No Property found','locality'),
		'not_found_in_trash' => __('No Property found in Trash','locality'), 
		'parent_item_colon' => ''
	  );
	  
	  $args = array(
		'labels' => $labels,
		'public' => true,
		'exclude_from_search' => false,
		'publicly_queryable' => true,
		'show_ui' => true, 
		'query_var' => true,
		'capability_type' => 'post',
		'hierarchical' => false,
		'menu_position' => 5,
		'supports' => array('title','editor','thumbnail','author'),
		'rewrite' => array( 'slug' => __('property', 'locality') )
	  ); 
	  
	  register_post_type('property',$args);
}
add_action( 'init', 'create_property_post_type' );

/* Create Property Type Taxonomy */
function build_taxonomies(){
    $labels = array(
        'name' => __( 'Property Feature', 'locality' ),
        'singular_name' => __( 'Property Feature', 'locality' ),
        'search_items' =>  __( 'Search Property Features', 'locality' ),
        'popular_items' => __( 'Popular Property Features', 'locality' ),
        'all_items' => __( 'All Property Features', 'locality' ),
        'parent_item' => __( 'Parent Property Feature', 'locality' ),
        'parent_item_colon' => __( 'Parent Property Feature:', 'locality' ),
        'edit_item' => __( 'Edit Property Feature', 'locality' ), 
        'update_item' => __( 'Update Property Feature', 'locality' ),
        'add_new_item' => __( 'Add New Property Feature', 'locality' ),
        'new_item_name' => __( 'New Property Feature Name', 'locality' ),
        'separate_items_with_commas' => __( 'Separate Property Features with commas', 'locality' ),
        'add_or_remove_items' => __( 'Add or remove Property Features', 'locality' ),
        'choose_from_most_used' => __( 'Choose from the most used Property Features', 'locality' ),
        'menu_name' => __( 'Property Features', 'locality' )
    );
    
	register_taxonomy(
	    'property-feature', 
	    array('property'), 
	    array(
	        'hierarchical' => false, 
	        'labels' => $labels,
	        'show_ui' => true,
	        'query_var' => true,
	        'rewrite' => array('slug' => __('property-feature', 'locality'))
	    )
	);
	
	$type_labels = array(
        'name' => __( 'Property Type', 'locality' ),
        'singular_name' => __( 'Property Type', 'locality' ),
        'search_items' =>  __( 'Search Property Types', 'locality' ),
        'popular_items' => __( 'Popular Property Types', 'locality' ),
        'all_items' => __( 'All Property Types', 'locality' ),
        'parent_item' => __( 'Parent Property Type', 'locality' ),
        'parent_item_colon' => __( 'Parent Property Type:', 'locality' ),
        'edit_item' => __( 'Edit Property Type', 'locality' ), 
        'update_item' => __( 'Update Property Type', 'locality' ),
        'add_new_item' => __( 'Add New Property Type', 'locality' ),
        'new_item_name' => __( 'New Property Type Name', 'locality' ),
        'separate_items_with_commas' => __( 'Separate Property Types with commas', 'locality' ),
        'add_or_remove_items' => __( 'Add or remove Property Types', 'locality' ),
        'choose_from_most_used' => __( 'Choose from the most used Property Types', 'locality' ),
        'menu_name' => __( 'Property Types', 'locality' )
    );
	
	register_taxonomy(
	    'property-type', 
	    array( 'property' ), 
	    array(
	        'hierarchical' => false, 
	        'labels' => $type_labels,
	        'show_ui' => true,
	        'query_var' => true,
			'rewrite' => array('slug' => __('property-type', 'locality'))
	    )
	);
	
	$city_labels = array(
        'name' => __( 'Property City', 'locality' ),
        'singular_name' => __( 'Property City', 'locality' ),
        'search_items' =>  __( 'Search Property Cities', 'locality' ),
        'popular_items' => __( 'Popular Property Cities', 'locality' ),
        'all_items' => __( 'All Property Cities', 'locality' ),
        'parent_item' => __( 'Parent Property City', 'locality' ),
        'parent_item_colon' => __( 'Parent Property City:', 'locality' ),
        'edit_item' => __( 'Edit Property City', 'locality' ), 
        'update_item' => __( 'Update Property City', 'locality' ),
        'add_new_item' => __( 'Add New Property City', 'locality' ),
        'new_item_name' => __( 'New Property City Name', 'locality' ),
        'separate_items_with_commas' => __( 'Separate Property Cities with commas', 'locality' ),
        'add_or_remove_items' => __( 'Add or remove Property Cities', 'locality' ),
        'choose_from_most_used' => __( 'Choose from the most used Property Cities', 'locality' ),
        'menu_name' => __( 'Property Cities', 'locality' )
    );
	
	register_taxonomy(
	    'property-city', 
	    array( 'property' ), 
	    array(
	        'hierarchical' => false, 
	        'labels' => $city_labels,
	        'show_ui' => true,
	        'query_var' => true,
			'rewrite' => array('slug' => __('property-city', 'locality'))
	    )
	);  
}
add_action( 'init', 'build_taxonomies', 0 );

/* Add Custom Columns */
function property_edit_columns($columns)
{

	$columns = array(
		  "cb" => "<input type=\"checkbox\" />",
		  "title" => __( 'Property Title','locality' ),		  
		  "thumb" => __( 'Thumbnail','locality' ),		  
		  "address" => __('Address','locality'),
		  "price" => __('Price','locality'),
		  "bed" => __('Beds','locality'),
		  "bath" => __('Baths','locality'),
		  "garage" => __('Garages','locality'),
		  "features" => __('Features','locality'),
		  "date" => __( 'Publish Time','locality' )
	);
	
	return $columns;
}
add_filter("manage_edit-property_columns", "property_edit_columns");


function property_custom_columns($column){
	global $post;
	switch ($column)
	{
		case 'thumb':
			if(has_post_thumbnail($post->ID))
			{
					?>
					<a href="<?php the_permalink(); ?>" target="_blank">
					 <?php the_post_thumbnail(array(70,70));?>
					</a>
					<?php
			}
			else
			{
				_e('No Thumbnail','locality');
			}
			break;
		case 'address':
			$address = get_post_meta($post->ID,'locality_property_location',true);
			if(!empty($address))
			{
				echo $address;
			}
			else
			{
				_e('No Address specified!','locality');
			}
			break;
		case 'price':
			$price = get_post_meta($post->ID,'locality_property_price',true);
			if(!empty($price))
			{
				echo number_format(intval($price));
			}
			else
			{
				_e('NA','locality');
			}
			break;
		case 'bed':
			$bed = get_post_meta($post->ID,'locality_property_bedrooms',true);
			if(!empty($bed))
			{
				echo $bed;
			}
			else
			{
				_e('NA','locality');
			}
			break;
		case 'bath':
			$bath = get_post_meta($post->ID,'locality_property_bathrooms',true);
			if(!empty($bath))
			{
				echo $bath;
			}
			else
			{
				_e('NA','locality');
			}
			break;
		case 'garage':
			$garage = get_post_meta($post->ID,'locality_garage',true);
			if(!empty($garage))
			{
				echo $garage;
			}
			else
			{
				_e('NA','locality');
			}
			break;
		case 'features':
			echo get_the_term_list($post->ID,'property-feature', '', ', ','');			
			break;
	}
}
add_action("manage_posts_custom_column", "property_custom_columns");
